package com.mesnac.comm.json;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;
import net.sf.json.xml.XMLSerializer;

import org.apache.commons.beanutils.PropertyUtils;

import com.mesnac.user.entity.User;

public class JsonTest {

	
	public static void main(String args[]){
		//List
		boolean[] boolArray = new boolean[]{true,false,true};      
        JSONArray jsonArray1 = JSONArray.fromObject( boolArray );      
        System.out.println( jsonArray1 );      
       // prints [true,false,true]     
          
        List list = new ArrayList();      
        List b = new ArrayList();
        b.add("test");
        list.add( "first" );      
        list.add( "second" );  
        list.add(b);
        JSONArray jsonArray2 = JSONArray.fromObject( list );      
        System.out.println( jsonArray2 );      
       // prints ["first","second"]     

        JSONArray jsonArray3 = JSONArray.fromObject( "['json','is','easy']" );      
        System.out.println( jsonArray3 );      
       // prints ["json","is","easy"]  
        
        //Map
        Map map = new HashMap();      
        map.put( "name", "json" );      
        map.put( "bool", Boolean.TRUE );      
          
        map.put( "int", new Integer(1) );      
        map.put( "arr", new String[]{"a","b"} );      
        map.put( "func", "function(i){ return this.arr[i]; }" );      
        JSONObject json = JSONObject.fromObject( map );      
        System.out.println( json );      
       //{"func":function(i){ return this.arr[i]; },"arr":["a","b"],"int":1,"name":"json","bool":true}  
        
        
        //BEAN
        User a = new User();
        a.setUserid("chilei");
        a.setPassword("11111");
        a.setPrivilege("111");
        JSONObject jsonObject = JSONObject.fromObject(a);      
        System.out.println( jsonObject );  
        
        
        //BEANS
        List list2 = new ArrayList();   
        User a1 = new User();
        a1.setUserid("chilei");
        a1.setPassword("11111");
        a1.setPrivilege("111"); 
          
        User a2 = new User();
        a2.setUserid("chidddlei");
        a2.setPassword("11d111");
        a2.setPrivilege("1d11");
          
          
        list2.add(a1);   
        list2.add(a2);   
          
        JSONArray ja = JSONArray.fromObject(list2);   
        System.out.println( ja.toString() );   
       //[{"value":"xx","row":1,"col":1},{"value":"","row":2,"col":2}
        
        
        //String to json
        String json2 = "{\"password\":\"11111\",\"privilege\":\"111\",\"tel_number\":\"\",\"user_role\":0,\"userid\":\"chilei\",\"username\":\"\",\"work_time\":0}";      
        JSONObject jsonObject2 = JSONObject.fromObject(json2);
        Object bean = JSONObject.toBean( jsonObject );  
        try{
        	System.out.println(PropertyUtils.getProperty(bean, "userid"));
        }catch(Exception e){
        	e.printStackTrace();
        }
        
        //xml
        System.out.println("json字符串转xml字符串");
        String jsonStr = "{\"password\":\"123456\",\"username\":\"dd\"}";
        JSON json3 = JSONSerializer.toJSON( jsonStr ); 
        XMLSerializer xmlSerializer = new XMLSerializer();
        xmlSerializer.setRootName("user_info");
        xmlSerializer.setTypeHintsEnabled(false);
        String xml = xmlSerializer.write(json3);
        System.out.println("json--->xml \n" + xml);
        
	}
}
